<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="属性名称" prop="attributeName">
        <el-input
          v-model="queryParams.attributeName"
          clearable
          placeholder="请输入属性名称"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性标识" prop="attributeCode">
        <el-input
          v-model="queryParams.attributeCode"
          clearable
          placeholder="请输入属性标识"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="attributeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="75"/>
      <el-table-column align="center" label="属性名称" prop="attributeName"/>
      <el-table-column align="center" label="属性标识" prop="attributeCode"/>
      <el-table-column align="center" label="数据类型" prop="attributeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_data_type" :value="scope.row.attributeType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="寄存器起始地址" prop="modbusRegister"/>
      <el-table-column align="center" label="寄存器个数" prop="registerCount"/>
      <el-table-column align="center" label="寄存器类型" prop="modbusRegisterType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_register_type" :value="scope.row.modbusRegisterType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数据值类型" prop="modbusDataType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.iot_modbus_data_type" :value="scope.row.modbusDataType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="单位" prop="unit"/>
      <el-table-column align="center" label="缩放因子" prop="scaleFactor"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="备注" prop="remark"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="属性名称" prop="attributeName">
          <el-input v-model="form.attributeName" placeholder="请输入属性名称"/>
        </el-form-item>
        <el-form-item label="属性标识" prop="attributeCode">
          <el-input v-model="form.attributeCode" placeholder="请输入属性标识"/>
        </el-form-item>
        <el-form-item label="数据类型" prop="attributeType">
          <el-select v-model="form.attributeType" placeholder="请选择数据类型">
            <el-option
              v-for="dict in dict.type.iot_data_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Modbus寄存器地址" prop="modbusRegister">
          <el-input v-model="form.modbusRegister" placeholder="请输入Modbus寄存器地址"/>
        </el-form-item>
        <el-form-item label="寄存器类型" prop="modbusRegisterType">
          <el-select v-model="form.modbusRegisterType" placeholder="请选择寄存器类型">
            <el-option
              v-for="dict in dict.type.iot_register_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据值类型" prop="modbusDataType">
          <el-select v-model="form.modbusDataType" placeholder="请选择数据值类型">
            <el-option
              v-for="dict in dict.type.iot_modbus_data_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="缩放因子" prop="scaleFactor">
          <el-input v-model="form.scaleFactor" placeholder="请输入缩放因子"/>
        </el-form-item>
        <el-form-item label="寄存器个数" prop="registerCount">
          <el-input v-model="form.registerCount" placeholder="请输入寄存器个数"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addAttribute, delAttribute, getAttribute, listAttribute, updateAttribute} from "@/api/system/iot/attribute";

export default {
  name: "Attribute",
  dicts: ['iot_data_type', 'iot_register_type', 'iot_modbus_data_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 属性表格数据
      attributeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attributeName: undefined,
        attributeCode: undefined,
        productId: this.$route.params.productId
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attributeName: [
          {required: true, message: "属性名称不能为空", trigger: "blur"}
        ],
        attributeCode: [
          {required: true, message: "属性标识不能为空", trigger: "blur"}
        ],
        attributeType: [
          {required: true, message: "数据类型不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询属性列表 */
    getList() {
      this.loading = true;
      listAttribute(this.queryParams).then(response => {
        this.attributeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        attributeId: undefined,
        attributeName: undefined,
        attributeCode: undefined,
        attributeType: undefined,
        unit: undefined,
        remark: undefined,
        productId: this.$route.params.productId
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attributeId);
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attributeId = row.attributeId || this.ids;
      getAttribute(attributeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attributeId != undefined) {
            updateAttribute(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttribute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attributeIds = row.attributeId || this.ids;
      this.$modal.confirm('是否确认删除属性编号为"' + attributeIds + '"的数据项？').then(function () {
        return delAttribute(attributeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
